package master.userInterface;

import java.awt.event.*;

import javax.swing.*;

import master.QuantumRegister;
import master.Workspace;
import master.quantumComputerIO.network.QuantumNetworkException;
import master.quantumComputerIO.network.QuantumNetworkIO;

public class WorkspaceWindowMenuBar extends JMenuBar implements ActionListener{
	WorkspaceWindow myWindow;
	JMenu [] menus = {
			new JMenu("File"),
			new JMenu("Edit")
	};
	public WorkspaceWindowMenuBar(WorkspaceWindow k){
		myWindow = k;
		for(JMenu m : menus)
			this.add(m);
		
		menus[0].add(new JMenuItem("New..."));
		menus[0].add(new JMenuItem("Save..."));
		menus[0].add(new JMenuItem("Open..."));
		menus[0].add(new JMenuItem("Close..."));
		menus[1].add(new JMenuItem("Edit input register..."));
		
		for(JMenu menu : menus)
			for(int i = 0 ; i < menu.getItemCount() ; i++)
				menu.getItem(i).addActionListener(this);
		
		
	}
	
	public Workspace getActiveWorkspace(){//get the workspace to apply menu options to
		return myWindow.getFocussedWorkspace();
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == "New..."){//new
			NetworkCreationFrame f = new NetworkCreationFrame(myWindow.getWorkspaceContext());
			f.setVisible(true);
		}else if(e.getActionCommand() == "Close..."){//close
			myWindow.getWorkspaceContext().destroyWorkspace(
					getActiveWorkspace()
			);
		}else if(e.getActionCommand() == "Save..."){//save
			
			try {
				QuantumNetworkIO.saveDialog(
						myWindow.getFocussedWorkspace().getWorkingNetwork()
				);
			} catch (QuantumNetworkException e1) {
				
			}
			
		}else if(e.getActionCommand() == "Open..."){//open
			try {
				myWindow.getWorkspaceContext().createWorkspace(
						QuantumNetworkIO.loadDialog()
				);
			} catch (QuantumNetworkException e1) {
				
			}
		}else if(e.getActionCommand() == "Edit input register..."){
			QuantumRegister input = myWindow.getFocussedWorkspace().getWorkingNetwork().getInputRegister();
			input.display("Input Register: "+myWindow.getFocussedWorkspace().getName());
		}
		
	}
}
